package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.api.publics.dto.response.BooleanResDTO;
import com.jzo2o.common.constants.CommonStatusConstants;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.IdUtils;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.RegistrasieMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.model.dto.response.LoginResDTO;
import com.jzo2o.customer.service.IRegistrasieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 01 7月 2025 16:08
 */
@Service
public class RegistrasieServiceImpl extends ServiceImpl<RegistrasieMapper,ServeProvider> implements IRegistrasieService {

    @Autowired
    private SmsCodeApi smsCodeApi;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTool jwtTool;
    @Autowired
    private ServeProviderSettingsServiceImpl serveProviderSettingsService;
    /**
     * 机构注册
     *
     * @param dto
     */
    @Override
    public LoginResDTO institutionRegister(InstitutionRegisterReqDTO dto) {
        //先校验验证码是否存在
        if (StringUtils.isBlank(dto.getVerifyCode())){
            throw new BadRequestException("验证码错误，请重新获取");
        }
        //调用远程的publics服务校验验证码是否正确
        BooleanResDTO verify = smsCodeApi.verify(dto.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, dto.getVerifyCode());
        if (!verify.getIsSuccess()) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        //首先先校验手机号是否已经被注册过
        //根据手机号和用户类型先看是不是已经注册过
        ServeProvider serveProvider = this.lambdaQuery()
                .eq(ServeProvider::getPhone, dto.getPhone())
                .eq(ServeProvider::getType, UserType.INSTITUTION)
                .one();
        if (serveProvider != null) {
            throw new BadRequestException("该手机号已经注册，请登录");
        }
//        //为空，则进行注册
        serveProvider = new ServeProvider();
        //使用BCryptPasswordEncoder对密码进行加密
        String password = passwordEncoder.encode(dto.getPassword());
        serveProvider.setPassword(password);
        serveProvider.setPhone(dto.getPhone());
        serveProvider.setType(UserType.INSTITUTION);
        serveProvider.setStatus(CommonStatusConstants.USER_STATUS_NORMAL);
        serveProvider.setCode(IdUtils.getSnowflakeNextIdStr());
        boolean save = this.save(serveProvider);
        if (!save) {
            throw new BadRequestException("注册失败");
        }
        //同步到服务人员/机构配置信息表
        serveProviderSettingsService.add(serveProvider.getId(), UserType.INSTITUTION);
        // 生成登录token
        String token = jwtTool.createToken(serveProvider.getId(), serveProvider.getName(), serveProvider.getAvatar(), UserType.INSTITUTION);
        return new LoginResDTO(token);
    }
}
